﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Web.Configuration;
namespace Manager
{
    public class RSAHelper
    {
        string PublicKey, PrivateKey;
        RSACryptoServiceProvider rsaProvider;
        public string publicKeyDir = System.Configuration.ConfigurationManager.AppSettings["PublicKeyDir"].ToString();
        public string privateKeyDir = System.Configuration.ConfigurationManager.AppSettings["PrivateKeyDir"].ToString();
        public string signDir = System.Configuration.ConfigurationManager.AppSettings["SignDir"].ToString();
        public string WriteSign(string name)
        {
            byte[] bytes = Convert.FromBase64String(name+"x");
            byte[] signs=Sign(bytes, name);
            string sign= Convert.ToBase64String(signs);
            return sign;
        }
        public void WriteRSA(string xmlname,string key,string dir)
        {
            string filename = dir + "\\" + xmlname + ".xml";
            if (!File.Exists(filename))
            {
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                StreamWriter one = new StreamWriter(filename, true, UTF8Encoding.UTF8);
                one.Write(key);
                one.Flush();
                one.Close();
            }
        }
        public void Initial(string name)
        {
            //声明一个RSA算法的实例，由RSACryptoServiceProvider类型的构造函数指定了密钥长度为1024位
            rsaProvider = new RSACryptoServiceProvider(1024);
            //rsaProvider.KeySize = 128;
            //将RSA算法的公钥导出到字符串PublicKey中，参数为false表示不导出私钥
            PublicKey = rsaProvider.ToXmlString(false);
            //将RSA算法的私钥导出到字符串PrivateKey中，参数为true表示导出私钥
            PrivateKey = rsaProvider.ToXmlString(true);
            //rsaProvider.Clear();
            WriteRSA("public"+name, PublicKey, publicKeyDir);
            WriteRSA("private" + name, PrivateKey, privateKeyDir);
        }


        public byte[] EncryptData(byte[] data)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);
            //将公钥导入到RSA对象中，准备加密；
            rsa.FromXmlString(PublicKey);
            //对数据data进行加密，并返回加密结果；
            //第二个参数用来选择Padding的格式
            return rsa.Encrypt(data, false);
        }

        public byte[] DecryptData(byte[] data)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);
            //将私钥导入RSA中，准备解密；
            rsa.FromXmlString(PrivateKey);
            //对数据进行解密，并返回解密结果；
            return rsa.Decrypt(data, false);
        }

        public byte[] Sign(byte[] data,string name)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);
            //导入私钥，准备签名

            //将数据使用MD5进行消息摘要，然后对摘要进行签名并返回签名数据

            StreamReader sr = new StreamReader(privateKeyDir+"\\"+"private"+name+".xml", UTF8Encoding.UTF8);
            string readprivatekey = sr.ReadToEnd();
            sr.Close();
            //StreamReader sr2 = new StreamReader(publicKeyDir + "\\" + "public" + name + ".xml", UTF8Encoding.UTF8);
            //string readpublickey = sr2.ReadToEnd();
            //sr2.Close();
            rsa.FromXmlString(readprivatekey);
            //rsa.FromXmlString(readpublickey);
            return rsa.SignData(data, "MD5");
        }

        public bool Verify(byte[] data, byte[] Signature)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);
            //导入公钥，准备验证签名
            rsa.FromXmlString(PublicKey);
            //返回数据验证结果
            return rsa.VerifyData(data, "MD5", Signature);
        }
    }
}